package com.dsx.haha.filter;

import com.dsx.haha.utils.RedPrint;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.apache.shiro.SecurityUtils;

/**
 * @author dsx
 * @date 2021/3/16 12:28
 * @since JDK1.8
 */
//1、注意导包，Filter是 org.apache.dubbo.rpc.*;
//2、@Activate 表示这个扩展是否被激活使用,并且经过扩展的过滤器往下走，dubbo有好多过滤器
//3、新建文件，固定的META-INF/dubbo/org.apache.dubbo.rpc.Filter： 
//4、文件中写DubboConsumerFilter=com.dsx.haha.filter.DubboConsumerFilter  过滤器名=包名。类名
//5、springboot的dubbo扩展配置就在application.yml或者properties中写    
@Activate    
public class DubboConsumerFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        //before
        RedPrint.print("-----------------------"+SecurityUtils.getSubject().getSession().getId().toString());
        RpcContext.getContext().setAttachment("sessionid", SecurityUtils.getSubject().getSession().getId().toString());
        RedPrint.print("consumer中发送的sessionid====="+SecurityUtils.getSubject().getSession().getId().toString());
        Result result = invoker.invoke(invocation);
        //after
        return result;//表示继续走其他过滤
    }
}
